package com.bukkit.feverdream.noon;

/**
 *
 * @author Aaron
 */
import java.util.TimerTask;
import org.bukkit.plugin.Plugin;

public class SunshineTimerTask extends TimerTask {

    private Plugin _plugin = null;
    private String _mode = "day";

    public SunshineTimerTask(Plugin plugin) {
        _plugin = plugin;
    }

    public void run() {
        long serverTime = _plugin.getServer().getTime();
        long relativeTime = serverTime % 24000;
        long startOfDay = serverTime - relativeTime;

        if (relativeTime > _getModeStopTime(_mode)) {
            _plugin.getServer().setTime(startOfDay + _getModeStartTime(_mode));
        }
    }

    public void setMode(String mode) {
        _mode = mode;

        //immediately set the time.
        long serverTime = _plugin.getServer().getTime();
        long relativeTime = serverTime % 24000;
        long startOfDay = serverTime - relativeTime;
        _plugin.getServer().setTime(startOfDay + _getModeStartTime(_mode));
    }

    public String getMode() {
        return _mode;
    }

    private long _getModeStartTime(String mode) {
        int result = 0;
        if (!mode.isEmpty()) {
            if (mode.equalsIgnoreCase("day")) {
                result = 0;
            } else if (mode.equalsIgnoreCase("sunset")) {
                result = 12000;
            } else if (mode.equalsIgnoreCase("night")) {
                result = 13800;
            } else if (mode.equalsIgnoreCase("sunrise")) {
                result = 22200;
            }
        }
        return result;
    }

    private long _getModeStopTime(String mode) {
        int result = 0;
        if (!mode.isEmpty()) {
            if (mode.equalsIgnoreCase("day")) {
                result = 12000;
            } else if (mode.equalsIgnoreCase("sunset")) {
                result = 13900;
            } else if (mode.equalsIgnoreCase("night")) {
                result = 22200;
            } else if (mode.equalsIgnoreCase("sunrise")) {
                result = 0;
            }
        }
        return result;
    }
}
